<template>
  <div class="common-header">
    <div class="header-content">
      <div class="logo">Doss直聘</div>
      <el-menu
        :default-active="activeIndex"
        mode="horizontal"
        :ellipsis="false"
        @select="handleSelect"
      >
        <el-menu-item index="childHome">首页</el-menu-item>
        <el-menu-item index="childJob">找工作</el-menu-item>
        <el-menu-item index="childEnterprise">找企业</el-menu-item>
        <el-menu-item index="childAbout">关于我们</el-menu-item>
      </el-menu>
      <div class="right-section">
        <el-button 
          v-if="globalData && globalData.token"
          round
          class="login-btn" 
          @click="logout"
        >退出</el-button>
        <el-button 
          v-else
          round
          class="login-btn" 
          @click="()=>router.push({name:'login'})"
        >登录</el-button>
        
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import router from '../router';
import loginApi from '../api/loginApi';
import microApp from '@micro-zoe/micro-app'
import { ElMessage } from 'element-plus'

const activeIndex = ref('childHome')
const globalData = microApp.getGlobalData()

const handleSelect = (index) => {
  router.push({ name: index })
}
const logout = () => {
  loginApi.logout({}).then(res =>{
    if(res.success){
      microApp.clearGlobalData()
      ElMessage.success('退出成功')
    }else{
      ElMessage.success('退出失败')
    }
  })
  router.push({ name: 'login' })
}
</script>

<style scoped>
.common-header {
  height: 60px;
  background-color: #fff;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.header-content {
  max-width: 1200px;
  height: 100%;
  margin: 0 auto;
  display: flex;
  align-items: center;
  padding: 0 20px;
}

.logo {
  font-size: 24px;
  font-weight: bold;
  color: #5dd5c8;
  margin-right: 40px;
  cursor: pointer;
}

:deep(.el-menu) {
  flex: 1;
  border: none;
  background-color: transparent;
}

:deep(.el-menu--horizontal) {
  border-bottom: none;
}

:deep(.el-menu-item) {
  font-size: 16px;
  height: 60px;
  line-height: 60px;
  color: #333;
}

:deep(.el-menu-item:hover) {
  color: #5dd5c8 !important;
}

:deep(.el-menu-item.is-active) {
  color: #5dd5c8 !important;
  font-weight: bold;
  border-bottom: 2px solid #5dd5c8;
}

.right-section {
  display: flex;
  align-items: center;
  margin-left: 20px;
}

.login-btn {
  background-color: #5dd5c8;
  color: #fff;
  border: none;
  padding: 8px 24px;
  font-size: 14px;
}

.login-btn:hover {
  background-color: #4cc3b6;
  color: #fff;
}
</style>